<template>
	<view class="container">
		<view class="content">
			<header-view></header-view>
			<view class="title">搜索</view>
			<view class="form">
				<view class="form_item">
					<view class="input_box flex">
						<input type="text" v-model="searchTxt" style="flex:1" maxlength="20" placeholder="输入搜索关键词"
							@input="handleSearch" />
						<uni-icons custom-prefix="iconfont" type="icon-duigou" size="24"></uni-icons>
					</view>
				</view>
			</view>
			<view class="sort">
				<view class="sort_title">
					搜索结果
				</view>
				<view class="list">
					<view class="item" v-for="item in list" @tap="openDetail(item.id)">
						{{item.name}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import headerView from "@/components/headerView.vue"
	import {
		stockListApi
	} from '@/api/api.js'
	export default {
		components: {
			headerView
		},
		data() {
			return {
				list: [],
				searchTxt: ""
			};
		},
		onLoad() {},
		watch: {
			searchTxt: {
				handler: 'handleSearch',
				immediate: true, // 立即执行一次
			},
		},
		methods: {
			handleSearch() {
				// 设置延时，避免频繁请求
				if (this.timer) {
					clearTimeout(this.timer);
				}

				this.timer = setTimeout(() => {
					this.getStockList();
				}, 300); // 设置延时时间，单位为毫秒，可以根据需要调整
			},
			// 获取产品列表
			getStockList() {
				const token = uni.getStorageSync('token');
				const keyword = this.searchTxt;
				stockListApi({
					token,
					keyword,
				}).then(res => {
					// 处理接口返回的结果
					this.list = res.data.data.data
				})
			},
			openDetail(opt) {
				const id = opt
				uni.navigateTo({
					url: "/pages/detail/index?id=" + id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		height: auto;

		.content {
			padding-bottom: 40rpx;
			// position: relative;

			.title {
				padding-top: 60rpx;
				font-size: 30px;
				font-weight: 500;
				font-family: PM;
				line-height: 92rpx;
			}

			.form {
				margin-top: 60rpx;

				.form_item {

					.form_title {
						font-family: PM;
						opacity: .8;
					}

					.input_box {
						width: 100%;
						margin-top: 20rpx;
						background-color: hsla(0, 0%, 92.9%, .46);
						border-radius: 20rpx;
						font-size: 30rpx;
						padding: 0 40rpx;
						height: 98rpx;
						line-height: 98rpx;
						border: 4rpx solid #3d3d3d;
						font-weight: 600;
						font-family: PM;
					}
				}

				.fixed {
					.btn {
						height: 100rpx;
						line-height: 100rpx;
						color: #fff;
						font-size: 32rpx;
						padding: 0 40rpx;
						border-radius: 20rpx;
						background-image: url("../../static/img/btn_bg.png");
						background-size: cover;
						background-repeat: no-repeat;
						overflow: hidden;
						// position: absolute;
						// right: 0;
						// left: 0;
						// bottom: 10vh;
					}

					.done {
						background-color: #0065ff;
						margin-left: 40rpx;
						margin-top: 40rpx;
						border-radius: 20rpx 0 0 20rpx;
					}
				}
			}

			.sort {
				margin-top: 50rpx;

				.sort_title {
					font-family: PM;
					font-size: 36rpx;
					margin-bottom: 40rpx;
				}

				.hot_list {
					column-gap: 20rpx;

					.hot_item {
						flex: 1;
						background-color: hsla(0, 0%, 92.9%, .46);
						border-radius: 20rpx;
						padding: 14rpx 0;
						text-align: center;
					}
				}

				.list {
					.item {
						font-family: PM;
						font-size: 36rpx;
						padding: 26rpx 0;
						border-bottom: 2rpx solid hsla(0, 0%, 92.9%, .46);
					}
				}
			}
		}
	}

	.mask {
		width: 100%;
		height: auto;
		background: #fff;
		border-radius: 40rpx 40rpx 0 0;
		padding: 40rpx;

		.back {
			width: 72rpx;
			height: 72rpx;
			background-color: hsla(0, 0%, 92.9%, .46);
			border-radius: 20rpx;
		}

		.title {
			font-size: 50rpx;
			margin: 40rpx 0;
		}

		.list {
			.item {
				margin: 20rpx 0;
				align-items: flex-start;

				.logo {
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
					overflow: hidden;
					box-shadow: 0px 3px 8px 0 rgba(0, 0, 0, .16);
					margin-right: 30rpx;
					flex-shrink: 0;
				}

				.i_list {
					flex: 1;
					overflow: hidden;
					box-shadow: 0px 3px 8px 0 rgba(0, 0, 0, .16);
					padding: 20rpx 30rpx;
					border-radius: 20rpx;

					.i_item {
						padding: 10rpx 0;
						font-weight: 600;

						.font_title {
							font-size: 32rpx;
							opacity: 0.6;
							font-weight: 500;
						}
					}
				}
			}
		}
	}
</style>